﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EducationUserGrid.aspx.cs" Inherits="Education_EducationUserGrid" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>教工档案</title>
    
    <link href="../Common/css/main.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #load{width:500px;height:25px;border:1px #000 solid;}
        #loading{position:absolute;z-index:2;height:23;filter:progid:DXImageTransform.microsoft.gradient(gradienttype=1,startColorStr=white,endColorStr=#39867b);}
        #loadtext{position:absolute;z-index:3;width:100%;height:100%;line-height:23px;text-align:center;}
    </style>
    
    <script type="text/javascript" src="../Common/Js/jquery.js"></script>

    <script type="text/javascript" src="../Common/Js/common.js"></script>

    <script type="text/javascript" src="../Common/Js/msg.js"></script>

    <script type="text/javascript" src="../Common/Js/jsUtil.js"></script>
    
    <script type="text/javascript"> 
    
    var optionType = '';
    var editUserID = 0;
    var queryType = '0';
    var iSchoolArea = -1;
    var iRightRowNum = 4;
    
    function ShowAddWindow()
    {
    	optionType = 'add';
     
        //显示添加用户信息对话框
        ShowPopForm("popTable", "formTitle", "添加用户信息");
        //显示背景遮罩
        ShowMask();        
       // self.parent.frames['left'].activate = 0;
    }
    
    function ShowUpdateWindow(obj)
    {
        optionType = 'update';
        ShowPopForm("popTable", "formTitle", "修改用户信息");
        
        ShowMask();
        //self.parent.frames['left'].activate = 0;
        
        var userId = obj.id;
        editUserID = userId;
        
        PageMethods.SelectUser(userId,function(succ)
        {
            if(succ.length <=0)
                return;   
                
            document.getElementById('username').value = succ.UserName;
            document.getElementById('username').setAttribute("oldCode",succ.UserName);
            
            document.getElementById('usertruename').value = succ.UserActualName;
            document.getElementById('usertruename').setAttribute("oldCode",succ.UserActualName);   
            
            if (succ.Phone == null)
            {
                succ.Phone = "";
            }
            document.getElementById('userphone').value = succ.Phone;
            document.getElementById('userphone').setAttribute("oldCode",succ.Phone);
            
            BindRight();
             	
        },function(err){//异步失败
            showMassgeBox("提示","查询失败",1,null);
        })
    	
    }
    
    function BindRight()
    {
        PageMethods.GetUserRight(editUserID,function(succ)
        {
            if (succ.length <= 0)
                return;
            
            var tableCheckBox = document.getElementById('userShowTable').getElementsByTagName("input");    
            
            for(var i = 0; i < succ.length; i++)
            {
                for(var j = 0; j < tableCheckBox.length; j++)
                {
                    if (tableCheckBox[j].type == "checkbox" && tableCheckBox[j].value == succ[i])
                    {
                        tableCheckBox[j].checked = 1;
                    }
                }
            }       
        },function(err)
        {
            showMassgeBox("提示","查询发生异常",1,null);
        })    	
    }
    
    function GetCheckRight()
    {
        var Rights = new Array();  
        
        var tableCheckBox = document.getElementById('userShowTable').getElementsByTagName("input"); 
        
        for(var i = 0; i < tableCheckBox.length; i++)
        {
            if (tableCheckBox[i].type == "checkbox" && tableCheckBox[i].checked == 1)
            {
                Rights.push(tableCheckBox[i].value);
            }
        }
        
        return Rights;       
    	
    }
    
    function CleanCheckState()
    {
        var tableCheckBox = document.getElementById('userShowTable').getElementsByTagName("input"); 
        
        for(var i = 0; i < tableCheckBox.length; i++)
        {
            if (tableCheckBox[i].type == "checkbox")
            {
               tableCheckBox[i].checked = 0;
            }
        }
    }
    
    function DeleteList()
    {
    	var arr = GetSelectUser();
        if(arr.length > 0)
        {
            //进行批量删除
            showConfirmBox("删除", "此操作会彻底删除选中用户信息，确定删除?", delUserInfo, arr);
        }
        else
            showMassgeBox("提示","请选择您要删除的用户");
    }
    
    function delUserInfo(arr)
    {
        PageMethods.DeleteUsers(arr,function(succ){
            showMassgeBox("提示","删除成功",1,null);
            Bind(0,"");
        },function(err){
           showMassgeBox("提示","发生异常，删除失败",1,null);
        });
    }
    
    function ResetPassword()
    {
        var arr = GetSelectUser();
        if(arr.length > 0)
        {
            //进行批量删除
            showConfirmBox("修改", "此操作会初始化选中的用户密码，确定更改?", ResetUserPwd, arr);
        }
        else
            showMassgeBox("提示","请选择您要初始化密码的用户");
    }
    
    function ResetUserPwd(arr)
    {
        PageMethods.ResetUserPwd(arr,function(succ){
            showMassgeBox("提示","重置密码成功",1,null);
            Bind(0,"");
        },function(err){
           showMassgeBox("提示","重置密码失败",1,null);
        });
    }

    function GetSelectUser()
    {
        var users = new Array();
        
        if(document.getElementById("userTable") != null)
        {
            var checkItems=document.getElementById("userTable").getElementsByTagName('input');
　　        for(var i=1;i < checkItems.length;i++){
　　　　        var checkItem= checkItems[i];
　　　　        if(checkItem.type==='checkbox' && checkItem.checked)
　　　　        {
　　　　            users.push(checkItem.userid);
　　　　        }
　　        }
　　    }
　　    
　　    return users;            	
    }   
    
    function queryTypeChange(obj)
    {
    	if (obj.value == '0')
        {
            document.getElementById('queryKeyWord').disabled = true;
        }
        else if (obj.value == '1')
        {
            document.getElementById('queryKeyWord').disabled = false;
        }
        else if (obj.value == '2')
        {
            document.getElementById('queryKeyWord').disabled= false;
        }
        
        document.getElementById('queryKeyWord').value = "";   
        queryType = obj.value;
    }
    
    function QueryUser()
    {   	
    }
    
    function Save()
    {
    	var userName = document.getElementById('username').value.Trim();
        var userRealName = document.getElementById('usertruename').value.Trim();
        var userPhone = document.getElementById('userphone').value.Trim();
       
        var iRights = GetCheckRight();
            
        //判断工号是否为空
        if (userName == "")
        {
            showNoticMsg('username','用户名不能为空，请重新输入');
            document.getElementById('username').select();  
            return;  
        }
        //判断姓名是否为空
        if (userRealName == "")
        {
            showNoticMsg('usertruename','用户实名不能为空，请重新输入');
            document.getElementById('usertruename').select();  
            return;  
        }
        if (getStringCount(userName) > 20)
        {
            showNoticMsg('username','用户名不能超过二十个字符，请重新输入');
            document.getElementById('username').select();  
            return;  
        }
        if (containSpecialChar(userName))
        {
            showNoticMsg('username','用户名存在特殊字符，请重新输入');
            document.getElementById('username').select();  
            return;  
        }
        
        if (getStringCount(userRealName) > 20)
        {
            showNoticMsg('usertruename','用户实名不能超过二十个字符，请重新输入');
            document.getElementById('usertruename').select();  
            return;  
        }
        
        if (containSpecialChar(userRealName))
        {
            showNoticMsg('usertruename','用户实名存在特殊字符，请重新输入');
            document.getElementById('usertruename').select();  
            return;  
        }
        if(userPhone != "")
        {
            if (!isTelephoneNumber(userPhone))
            {
                showNoticMsg('userphone','电话号码必须为数字');
                document.getElementById('userphone').select();  
                return;
            }
        }

        if(optionType == "add")
        {
           Add(userName,userRealName,userPhone,iRights);
        }
        if(optionType == "update")
        {
           Update(userName,userRealName,userPhone,iRights);
        }
    }
    
    function jsright(rights,rightn) 
    { 

        var sr=rights; 

        sr = sr.substring(sr.length-rightn,sr.length); 

        return sr; 
    }
    
    function InitPremInfo()
    {
        PageMethods.GetUserSystemPrm(1,function(succ)
        {
            if (succ.length > 0)
            {
                var table = document.getElementById('userShowTable');
                for(var i=0; i < succ.length; i++)
                {
                    //往表格插入一行，并插入子系统名称
                    var newTr = table.insertRow(table.rows.length - 2);
                	var objTd = newTr.insertCell(0);
                	objTd.style.textAlign = "left";
                    //objTd.style.width = "100%";
                    objTd.style.valign="top";
                	objTd.setAttribute("colSpan","4");
                	
                	var strPremInnerHTML = succ[i].SystemName;
                	
                	if (succ[i].PremList.length > 0)
                	{
                	    strPremInnerHTML = strPremInnerHTML + "<br /><table border='0'>";
                	    
                	    var iClsNum = 0;
                	    for(var j = 0; j < succ[i].PremList.length; j++)
                	    {
                	        if(iClsNum%iRightRowNum == 0)
                	        {
                	            strPremInnerHTML = strPremInnerHTML + "<tr><td style='width:27px;'></td>";     	        
                	        }
                	        
                	        var strRightName = "";
                	        var strFullRightName = "";
                	        if (succ[i].PremList[j].PremName.length > 8)
                	        {
                	            strRightName =  succ[i].PremList[j].PremName.substring(0,8) + "...";
                	            strFullRightName = succ[i].PremList[j].PremName;                	                           	            
                	        }
                	        else
                	            strRightName = succ[i].PremList[j].PremName;
                	                                                                                                                                                                                    
                	        strPremInnerHTML = strPremInnerHTML + "<td style ='text-align: left; width:140;'><input id='"+succ[i].PremList[j].PremNo+"' type='checkbox' name='userRight' value='"+succ[i].PremList[j].PremNo+ "'/><font title=\""+ strFullRightName +"\">&nbsp;"+strRightName +"</font></td>";
                	        iClsNum = iClsNum +1;
                	        
                	         //判断是否是3、6、9....如果是，则加入行尾符
    	                    if (iClsNum != 0 && iClsNum%iRightRowNum == 0)
    	                    {
    	                        strPremInnerHTML = strPremInnerHTML + "</tr>";
    	                        iClsNum == 0;
    	                    }      
                	    }
                	    //判断最后五个字符，是否等于</tr>,不是则添加</tr>
            	        if (jsright(strPremInnerHTML,5) != "</tr>")
            	            strPremInnerHTML = strPremInnerHTML + "</tr>";                	        
            	        
            	        strPremInnerHTML = strPremInnerHTML + "</table>" ;
                	}
                	
                	objTd.innerHTML = strPremInnerHTML;
                	
                }
            }        	
        },function(fail)
        {
            showMassgeBox("提示","初始化失败",1,null);
        })        	
    }
    
    function Add(userName,userRealName,userPhone,iRights)
    {
        var UserEntiy = {RID:0, AreaID:iSchoolArea,UserName:userName,UserActualName:userRealName,Phone:userPhone,IsAdmin:0};   	

        PageMethods.ValidateUserInfo(UserEntiy,function(succ){ 
           if(succ > 0)
           {
                if (succ == 1)
                {
                    showNoticMsg('username','用户名已经存在，请重新设置');
                    document.getElementById('username').select();  
                }
           }
           else
           {  
                PageMethods.InsertUser(UserEntiy,iRights,function(succ){
                    if(succ > 0)
                    {
                        Bind(0,"");
                        CloseMask('popTable');
                    }
                    else
                    {
                        showMassgeBox("提示","用户信息添加失败",1,null);
                    }
                },function(err){
                    showMassgeBox("提示","用户信息添加失败",1,null);
                }); 
           }      
        },function(err){
                showMassgeBox("提示","用户信息验证过程失败，您可以刷新后重试",1,null);
        });        
        
    }
    
    function Update(userName,userRealName,userPhone,iRights)
    {
    	var UserEntiy = {RID:editUserID, AreaID:iSchoolArea,UserName:userName,UserActualName:userRealName,Phone:userPhone,IsAdmin:0};  
    	 
        var oldName = document.getElementById('username').getAttribute("oldCode");
        //判断教工工号是否修改，先验证后更新
        if (oldName != userName)
        {
            PageMethods.ValidateUserInfo(UserEntiy,function(succ){ 
               if(succ > 0)
               {
                    if (succ == 1)
                    {
                        showNoticMsg('username','用户名已经存在，请重新设置');
                        document.getElementById('username').select();  
                    }
               }
               else
               {  
                    PageMethods.UpdateUser(UserEntiy,iRights,function(succ){
                        if(succ > 0)
                        {
                            Bind(0,"");
                            CloseMask('popTable');
                        }
                        else
                        {
                            showMassgeBox("提示","用户信息修改失败",1,null);
                        }
                    },function(err){
                        showMassgeBox("提示","用户信息修改失败",1,null);
                    }); 
               }
            },function(err){
                 showMassgeBox("提示","用户信息验证过程失败，您可以刷新后重试",1,null);
            });
        }
        //没有修改的情况直接更新
        else
        {
            PageMethods.UpdateUser(UserEntiy,iRights,function(succ){
                if(succ > 0)
                {
                    Bind(0,"");
                    CloseMask('popTable');
                }
                else
                {
                    showMassgeBox("提示","用户信息修改失败",1,null);
                }
            },function(err){
                showMassgeBox("提示","用户信息修改失败",1,null);
            }); 
        }    	
    	
    }
    
    function ShowMask()
    {
        showMask();
        self.parent.frames['left'].showMask();
    }
    
    //全选
    function CheckedAll(obj)
    {
        var checkItems=document.getElementById("userTable").getElementsByTagName('input');
        for(var i=0;i < checkItems.length;i++){
　　　　    var checkItem= checkItems[i];
　　　　    if(checkItem.type==='checkbox'){
　　　　　    checkItem.checked = obj.checked ;
　　　　    }
　　    }
    }
    
    function CloseMask(formId)
    {
        self.parent.frames['left'].activate = 1;
        editUserID = 0;
        
        ReleasePopTable();
        ClosePopForm(formId,true);
        hideMask();
        self.parent.frames['left'].hideMask();
    }
            
    function ReleasePopTable()
    {
        document.getElementById('username').value = "";
        document.getElementById('usertruename').value = ""
        
        document.getElementById("userphone").value = "";
        CleanCheckState();
        //document.getElementById("examRight").checked = 0;
        //document.getElementById("selectExamRight").checked = 0;   
    }
    
    function Load()
    {
        var arr = new Array();
        var u = window.location.toString();
        u = u.split('?');
        if (typeof(u[1]) == 'string') {
            u=u[1].split('&');
            for(var i=0;i<u.length;i++)
            {
                s=u[i].split("=");
                eval('arr["' + s[0] + '"]' + '="' + s[1]+'"');
            }
        }
        var id = arr['id'];
        iSchoolArea = id;
        
        if (id != null)
        { 
            Bind(0,"");
            InitPremInfo();
        }
    }
    
    function Bind(queryType,queryValue)
    {
    
        //查询条件
        document.getElementById('HFSchoolAreaValue').value = iSchoolArea;
        document.getElementById('HFQueryType').value = queryType;
        document.getElementById('HFQueryValue').value = queryValue;
        document.getElementById('btnBind').click();
    }
    
//    function Bind()
//    {
//        
//        PageMethods.BindDataByAreaID(iSchoolArea, function(succ)
//        {
//            if (succ == -1)
//            {
//                return;                
//            }
//            else
//            {
//                document.getElementById('btnBind').click();  
//            }        
//        }, function(err)
//        {
//            showMassgeBox("提示","查询失败",1,null);        	
//        })  
//    }

    function SetHightLight(obj)
    {
        obj.style.backgroundColor = "#fffccf";
        var table = document.getElementById('userTable');
        for(var i =0 ; i < table.rows.length;i++)
        {
            var row = table.rows[i];
            if(row != null && row.id != obj.id)
                row.style.backgroundColor = "#ffffff";

        }
    }    
    </script>
    
</head>
<body onload="Load();">
    <div class="mainWarp">
        <div class="pageBody">
            <form id="form1" runat="server">
                <div>
                    <ul class="butList">
                        <li>
                            <table width="100%" border="0">
                                <tr>
                                    <td width="50%" style="text-align: left;">
                                        <input type="button" id="button3" value="新增" onclick="ShowAddWindow();" class="commonBut" />
                                        <input type="button" id="btnDel" value="删除" class="commonBut" onclick="DeleteList();" />
                                        <input type="button" id="resetPass" value="重置密码" onclick="ResetPassword();" class="commonBut" />&nbsp;</td>
                                   <td width= "50%" style ="text-align: right;">
                                        <%-- <select id="queryType" style="width: 120px" onchange="queryTypeChange(this);">
                                            <option selected value="0" >所有</option>
                                            <option value="1">用户名</option>
                                            <option value="2">用户实名</option>
                                        </select>
                                        <input type="text" name="textfield" id="queryKeyWord" class="txtStyle" style="width: 120px" />
                                        <input id="btnquery" class="commonBut" onclick="QueryUser();" type="button" value="查询" />--%>
                                    </td>
                                </tr>
                            </table>
                        </li>
                    </ul>
                </div>
                <div>
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
                    </asp:ScriptManager>
                </div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Repeater ID="Repeater1" runat="server">
                            <HeaderTemplate>
                                <table id="userTable" border="0" cellpadding="0" cellspacing="1" class="useList">
                                    <tr class="tableTitle">
                                        <th width="2%" height="26">
                                            <input type="checkbox" onclick="CheckedAll(this);" /></th>
                                        <th width="10%" style="text-align: left; padding-left: 5px;">
                                            用户名</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;">
                                            用户实名</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;" >
                                            电话</th>
                                        <%=GetPremHeaderString(1) %>
                                        <th width="15%">
                                        </th>
                                        
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr id="<%# DataBinder.Eval(Container.DataItem, "RID") %>" 
                                    style="background-color: White;"  onclick="SetHightLight(this);">
                                    <td height="26">
                                        <input type="checkbox" userid="<%# DataBinder.Eval(Container.DataItem, "RID") %>" />
                                    </td>
                                    <td style="text-align: left; font-weight: normal;padding-left:5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "UserName")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal; padding-left: 5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "UserActualName")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal; padding-left: 5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "Phone")%>
                                    </td>
                                    <%# GetUserPrem((int)Eval("RID"))%>
                                    <td>
                                        <img src="../common/images/hadle_icon.gif" alt="修改用户信息" class="imageBtnStyle" onclick="ShowUpdateWindow(this.parentElement.parentElement);" />
                                    </td>
                                    
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                         <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Right"  PageSize="15" 
                            width="100%"   PagingButtonSpacing="10px" ButtonImageAlign="left" 
                            OnPageChanged="AspNetPager1_PageChanged"> </webdiyer:aspnetpager> 
                        </td> 
                        </tr>
                    </table>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnBind" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
                <div style="display: none;">
                    <asp:Button ID="btnBind" runat="server" OnClick="btnBind_Click" Text="btnBind" />
                    <asp:HiddenField id="HFSchoolAreaValue" runat="server" Value=""></asp:HiddenField>
                    <asp:HiddenField ID="HFQueryType" runat="server" Value="" />
                    <asp:HiddenField ID="HFQueryValue" runat="server" Value="" />
                </div>
            </form>
            <!-- 新增/修改学校弹出界面 开始-->
            <div id="popTable" class="layoutWarp" style="display: none; width: 600px; overflow: inherit;">
                <div class="title">
                    <span id="formTitle">新增</span> <a title="关闭" onclick="return CloseMask('popTable')">
                        关闭</a>
                </div>
                <table id = "userShowTable" style="width: 587px; margin-top: 12px;" border="0" class="layoutTable">
                    <tr>
                        <td valign="top" style="text-align: right; height: 26px; width: 77px;">
                             用户名:</td>
                        <td style="text-align: left; width: 147px; color:Red; height: 26px;">
                            <input type="text" name="textfield" id="username" class="txtStyle" maxlength="20" style="width: 120px" /> *</td>
                        <td valign="top" style="text-align: right; width: 77px;">用户实名:</td>
                        <td style="text-align: left; width: 147px; color:Red">
                        <input id="usertruename" name="textfield2" class="txtStyle" type="text" maxlength="20" style="width: 120px" /> *</td>
                    </tr>
                    <tr>
                        <td valign="top" style="text-align: right; width: 77px;">
                            电话号码:</td>
                        <td style="text-align: left; width: 147px; color:Red">
                            <input id="userphone" name="textfield2" class="txtStyle" type="text" maxlength="20" style="width: 120px" onkeypress="event.returnValue=((event.keyCode>=48)&&(event.keyCode<=57));" /></td>
                        <td valign="top" style="text-align: right; width: 77px;">
                            </td>
                        <td style="text-align: left; width: 147px; color:Red">
                            </td>
                    </tr> 
                   <%--                        <td rowspan="2" style="text-align: right; width: 77px;">成绩系统:
                        </td>
                        <td style="text-align: left; width: 147px">
                            <input type="checkbox" id="examRight" />统考管理
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width: 147px">
                            <input type="checkbox" id="selectExamRight" />查看成绩分析报表
                        </td>
                    </tr>  --%><tr>

<%--                    <tr>
                        <td colspan="3" valign="top">
                            <fieldset>
                                <legend>成绩系统</legend>
                                <div>
                                    <table id="Table1" style="width: 95%; text-align:left">
                                        <tr>
                                            <td style="width: 25%">
                                                <input id="examRight" type="checkbox" />统考管理</td>
                                            <td style="width: 35%">
                                                <input id="selectExamRight" type="checkbox" />查看成绩分析报表</td>
                                            <td style="width: 40%">
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </fieldset>
                        </td>
                    </tr>--%>
                    <tr>
                        <td colspan="4" align="center">
                            <ul class="butList">
                                <input type="button" id="btnsave" value="保存" onclick="Save()" class="commonBut" />
                                <input type="button" value="取消" onclick=" CloseMask('popTable');" class="commonBut" />
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- 新增/修改学校弹出界面 结束 -->
        </div>
    </div>

</body>
</html>

